<?php
###
#  Baing PHP 框架 1.0.0
#  http://baing.co/
#
#  Copyright: 白晓阳
#  License: CC BY-NC-SA 4.0
#  https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh
###

namespace bai\action;

use bai\feature\Agent;
use bai\action\View;


class Redirect extends \bai\Action
{
    public function run()
    {
        if (self::$request->method !== self::GET) return $this->next();

        $page = $this->find('page') ?? '/' . View::VIEW_INDEX;
        if ($page == self::$process->path) return $this->next();

        $url = self::$request::url($page);
        if ($this->find('forward') ?? true) {
            $forward = Agent::server(self::$request->ajax ? 'HTTP_REFERER' : 'REQUEST_URI');
            if ($forward) {
                $url .= '?forward=' . urlencode($forward);
            }
        }
        self::$response->addHeader('Location', $url);
        return $this->exit(quiet: true);
    }
}
